const { writeFile, readFile, getPathBlockSchema } = require("../../../app/util");
const { getAllSectionsSchema } = require("./sectionsSchema");
const { getGlobal } = require("./global");
const { getPagesSetting } = require("./page");
const { getBlocks } = require("./block");

function buildConfig(template) {
    let settingsData = { global: { sections: {} }, pages_settings: [] };
    let settingsScheme = { global_setting: [], pages_setting: [], blocks_setting: [] };

    //获取默认积木块配置
    const blocksSetting = getAllSectionsSchema(template);

    // 全局配置
    const { settingsGlobalData, global } = getGlobal(template);
    Object.assign(settingsData.global, settingsGlobalData);
    settingsScheme.global_setting = global;
    settingsData.global.sections["header"] = blocksSetting["header"].default;
    settingsData.global.sections["footer"] = blocksSetting["footer"].default;

    // 积木块配置
    const { blockContent, blocks_setting } = getBlocks(template, blocksSetting);
    settingsScheme.default_blocks_setting = blockContent.default_blocks_setting;
    settingsScheme.blocks_setting = blocks_setting;

    //生成页面配置
    const { pagesData, pagesSchema } = getPagesSetting(template,blocksSetting);
    settingsData.pages_settings = pagesData;
    settingsScheme.pages_setting = pagesSchema;
    writeFile(`./src/theme/${template}/config/settings_data.json`, JSON.stringify(settingsData, "", "\t"));
    writeFile(`./src/theme/${template}/config/settings_schema.json`, JSON.stringify(settingsScheme, "", "\t"));
}
module.exports = { buildConfig };
